const express = require('express')
//const static = require('express-static')
const cookieParser=require('cookie-parser')
const cookieSession=require('cookie-session')
const bodyParser=require('body-parser')
const multer= require('multer')
const mysql=require('mysql')

const db=mysql.createPool({host:'47.104.10.44',user:'root',password:'199166',database:'cxy-vueCms'})

const server= express()


server.all('*', function(req, res, next) {
       res.header("Access-Control-Allow-Origin", "*");
       res.header("Access-Control-Allow-Headers", "X-Requested-With");
       res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
       res.header("X-Powered-By",' 3.2.1');
       res.header("Content-Type", "application/json;charset=utf-8");
       next();
    });


server.get('/bannerTable',function(req,res){
    db.query("SELECT * FROM `banner_table`",(err,data)=>{
        if(err){
            console.log(err)
            res.send('server error')
        }else{
            console.log(data)
            res.send(data)
        }
    })
})

server.use('/getNewsList',function(req,res){
    db.query("SELECT * FROM `newsList`",(err,data)=>{
        if(err){
            console.log(err)
            res.send('server error')
        }else{
            console.log(data)
            res.send(data)
        }
    })
})

//解析cookie
server.use(cookieParser('qweasdzxc'));

//使用session
var arr = []
for(var i=0;i<10000;i++){
    arr.push('keys_'+Math.random())
}
server.use(cookieSession({name:'zns',keys:arr,maxAge:20*3600*1000}))

server.use(express.static('./libs'));
server.listen(4000)